package com.aaa.service.impl;

import com.aaa.dao.DeptDao;
import com.aaa.entity.CheckItem;
import com.aaa.entity.Dept;
import com.aaa.service.DeptService;
import com.aaa.util.AccountName;
import com.aaa.vo.DeptVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description:
 * @CreateTime: 2023-03-22  19:26
 * @Author: ql
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;
    @Override
    public Result<List<Dept>> findAll() {
        QueryWrapper<Dept> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Dept> list = deptDao.selectList(wrapper);
        return new Result<>(2000,"查询所有部门",list);
    }

    @Override
    public Result<IPage<Dept>> queryByPage(DeptVo deptVo) {
        IPage<Dept> deptPage = new Page<>(deptVo.getPageNum(),deptVo.getPageSize());
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(deptVo.getDeptName())){
            wrapper.like("dept_name",deptVo.getDeptName());
        }if (StringUtils.hasText(deptVo.getStatus())){
            wrapper.like("status",deptVo.getStatus());
        }if (StringUtils.hasText(deptVo.getBeginTime())&&StringUtils.hasText(deptVo.getEndTime())){
            wrapper.like("create_time",deptVo.getStatus());
        }
        IPage<Dept> deptIPage = deptDao.selectPage(deptPage, wrapper);

        return new Result<>(2000,"ok",deptIPage);
    }

    @Autowired
    private AccountName accountName;
    @Override
    public Result<String> addOrUpdDept(Dept dept) {

        String userName = accountName.getAccountName();
        System.out.println("添加或修改的====================== " + dept);
        int num;
        if (dept.getDeptId()!=null){
            dept.setUpdateBy(userName);
            dept.setUpdateTime(new Date());
            //修改
            num = deptDao.updateById(dept);
        }else {
            dept.setCreateBy(userName);
            dept.setCreateTime(new Date());
            num = deptDao.insert(dept);
        }
        if (num>0){
            return Result.success();
        }
        return Result.error();

    }



    @Override
    public Result<Dept> queryById(Long id) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("dept_id",id);
        Dept dept = deptDao.selectOne(wrapper);
        return new Result<>(2000,"ok",dept);

    }

    @Override
    public Result<String> deleteByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = deptDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Dept getDeptById(Integer id) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("dept_id",id);
        return deptDao.selectOne(wrapper);
    }
}
